package org.cms;

import org.cms.entities.*;
import org.cms.raw.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by elnoa on 29/10/14.
 */
public class CheckinMapper {

    public static CheckIn map(CheckInRaw fsCheckIn) {
        CheckIn ci = new CheckIn();
        ci.setId(fsCheckIn.getId());
        ci.setCreatedAt(fsCheckIn.getCreatedAt());
        ci.setTimeZone(fsCheckIn.getTimeZoneOffset());
        ci.setShout(fsCheckIn.getShout());

        ci.setVenue(venueMapper(fsCheckIn.getVenue()));
        return ci;
    }

    private static Venue venueMapper(VenueRaw fsVenue) {
        Venue venue = new Venue();
        venue.setId(fsVenue.getId());
        venue.setName(fsVenue.getName());
        //TODO check wether a list of category in the fsVenue are necessary to be a separat table
//        List<Category> categoryList = categoryMapper(fsVenue.getCategories());
//        if(categoryList.size()>0){
//            venue.setCategory(categoryList.get(0));
//        }
//        venue.setStats(statMapper(fsVenue.getStats()));
        venue.setLocation(locationMapper(fsVenue.getLocation()));
        return venue;
    }

    private static Stats statMapper(StatsRaw fsStats) {
        Stats stats = new Stats();
        stats.setCheckinsCount(fsStats.getCheckinsCount());
        stats.setUsersCount(fsStats.getUsersCount());
        return stats;
    }

    private static Location locationMapper(LocationRaw fsLocation) {
        String fsAdress = fsLocation.getAddress();
        if (fsAdress == null || "".equals(fsAdress)) {
            fsAdress = "NotDefined";
        }
        Location location = new Location(fsAdress);
        location.setCity(fsLocation.getCity());
        location.setCountry(fsLocation.getCountry());
        location.setPostalCode(fsLocation.getPostalCode());
        location.setState(fsLocation.getState());
        location.setLat(fsLocation.getLat());
        location.setLng(fsLocation.getLng());
        return location;
    }

    private static List<Category> categoryMapper(CategoriesRaw[] fsCategories) {
        List<Category> categoryList = new ArrayList<Category>();
        for (CategoriesRaw c : fsCategories) {
            Category category = new Category();
            category.setName(c.getName());
            categoryList.add(category);
        }
        return categoryList;
    }
}
